<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

class User extends Controller
{
    public function index()
    {
//        $list = db('user','')->field(['id','username','name'])->select();
//        $data = Db::field(['role.name r_name','user.name u_name','status'])
//                ->table('lamp_user user,lamp_role role,lamp_user_role usro')
//                ->where('user.id=usro.uid and role.id=usro.rid')
//                ->group('u_name')
//                ->select();
        //查询数据
//        where('username','like','$username%');
        var_dump($_GET);
        $wwb = input('param.uname');
        if (empty($wwb)) {
            $list = DB::name('user')->paginate(3);
        } else {

            $list = DB::name('user')->where('username', 'like', $wwb.'%')->paginate(3);
        }
        $arr = array(); //声明一个空数组
        //遍历用户信息
        foreach($list as $v){
            $role_ids = DB::name('user_role')->field('rid')->where(array('uid'=>array('eq',$v['id'])))->select();
            //定义空数组
            $roles = array();
            //遍历
            foreach($role_ids as $value){
                $roles[] = DB::name('role')->where(array('id'=>array('eq',$value['rid']),'status'=>array('eq',1)))->value('name');
            }
            $v['role'] = $roles; //将新得到角色信息放置到$v中
            $arr[] = $v;
        }
        $page = $list->render();

        //分配变量
        $this->assign("list",$arr);
        $this->assign("page",$page);

        return view('user/index');
    }

    public function insert()
    {
        if ($_POST['userpass'] !== $_POST['reuserpass']) {
            $this->error('两次密码不一致');
        }
        unset($_POST['reuserpass']);
        $_POST['userpass'] = md5($_POST['userpass']);

        $data = $_POST;
        $result = db('user')->insert($data);
        if (!$result) {
            $this->error('添加失败');
        }

        $this->success('新增成功','admin/User/index');

        // return view('admin@user/index');


    }

    public function delete($id)
    {
        $result = db('user')->delete($id);
        if (!$result) {
            $this->error('删除失败');
        }
        $this->success('删除成功','admin/user/index');
    }

    public function edit($id)
    {
        $list = db('user')->field(['id','username','name'])->find($id);
        return view('admin@user/edit',['data'=>$list]);
    }

    public function update(Request $request)
    {
        // var_dump($request);
        // var_dump($_POST); 
        $p = $request->param();
        // var_dump($p);
        $data = [
            'name' => $p['name'],
            'username' => $p['username']
        ];
        $result = db('user')->where('id',$p['id'])->update($data);
        if ($result) {
            $this->success('修改成功','admin/user/index');
        }
        $this->error('修改失败');


    }

    public function add()
    {
        return view('admin@user/add');
    }

    public function rolelist($id)
    {
        $name = db('user')->field('username,id')->find($id);
        $role = db('role')->where('status=1')->select();
        $data = Db::field(['role.name r_name','role.id rid'])
                ->table('lamp_user user,lamp_role role,lamp_user_role usro')
                ->where('user.id=usro.uid and role.id=usro.rid','role.status=1')
                ->select($id);
        $arr =array();
        foreach ($data as $k => $v) {
            $arr[] = $v['rid'];
        }

//            var_dump($arr);exit;
//
//        var_dump($data);
//        var_dump($name);
//        var_dump($role);
//        var_dump($arr);
        $this->assign("arr",$arr);
        $this->assign("name",$name);
        $this->assign("role",$role);
//        $this->assign("data",$data);
        return view('admin@/user/rolelist');
    }

    public function saverole($id)
    {
        if (empty($_POST))
        {
            $this->error('必须选择一个角色');
        }

        db('user_role')->where('uid='.$id)->delete();
        foreach ($_POST as $k => $v) {
            $data['uid'] = $id;
            $data['rid'] = $v;
            db('user_role')->insert($data);
        }

        $this->success('修改成功','admin/user/index');

    }

//    public function search（）
//    {
//
//    }

}